package cn.dankal.flyaudio.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.donkingliang.groupedadapter.adapter.GroupedRecyclerViewAdapter;
import com.donkingliang.groupedadapter.holder.BaseViewHolder;
import com.facebook.stetho.common.LogUtil;
import com.google.gson.Gson;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import cn.dankal.basiclib.base.recyclerview.BaseRecyclerViewAdapter;
import cn.dankal.basiclib.base.recyclerview.BaseRecyclerViewHolder;
import cn.dankal.basiclib.util.ActivityUtils;
import cn.dankal.basiclib.util.image.PicUtils;
import cn.dankal.flyaudio.FlyApplication;
import cn.dankal.flyaudio.R;
import cn.dankal.flyaudio.entity.ContractPersonEntity;
import cn.dankal.flyaudio.entity.GroupEntity;
import cn.dankal.flyaudio.entity.PickGroupEntity;
import cn.dankal.flyaudio.friend.ChatActivity;
import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.callback.GetAvatarBitmapCallback;
import cn.jpush.im.android.api.enums.ConversationType;
import cn.jpush.im.android.api.model.Conversation;
import cn.jpush.im.android.api.model.GroupInfo;
import cn.jpush.im.android.api.model.UserInfo;

public class ContractPersonAdapter extends GroupedRecyclerViewAdapter {

    public static final int MODEL_PICK = 1001;

    public static final int MODEL_NORMAL = 1002;

    private int pickModel = MODEL_NORMAL;
    private List<GroupEntity> datas;
    private Map<String, String> mDraftMap = new HashMap<>();

    private List<PickGroupEntity> pickPos = new ArrayList<>();

    private boolean isSinglePick = false;

    private OnItemSelectListener onItemSelectListener;

    public void setOnItemSelectListener(OnItemSelectListener listener) {
        onItemSelectListener = listener;
    }

    public List<GroupEntity> getData() {
        return datas;
    }

    public void setNewData(List<GroupEntity> groupEntities) {
        this.datas = groupEntities;
        notifyDataChanged();
    }

    public void setSinglePick(boolean isSinglePick) {
        this.isSinglePick = isSinglePick;
    }

    public ContractPersonAdapter(Context context, List<GroupEntity> groupEntities) {
        super(context);
        this.datas = groupEntities;
    }

    public void setPickModel(int pickModel) {
        this.pickModel = pickModel;
    }

    @Override
    public int getGroupCount() {
        return datas.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return datas.get(groupPosition).getChildren().size();
    }

    @Override
    public boolean hasHeader(int groupPosition) {
        return true;
    }

    @Override
    public boolean hasFooter(int groupPosition) {
        return false;
    }

    @Override
    public int getHeaderLayout(int viewType) {
        return R.layout.item_contract_person_head;
    }

    @Override
    public int getFooterLayout(int viewType) {
        return 0;
    }

    @Override
    public int getChildLayout(int viewType) {
        return R.layout.item_contract_person;
    }

    @Override
    public void onBindHeaderViewHolder(BaseViewHolder holder, int groupPosition) {
        ((TextView) holder.itemView).setText(datas.get(groupPosition).getHeader());
    }

    @Override
    public void onBindFooterViewHolder(BaseViewHolder holder, int groupPosition) {

    }

    public List<UserInfo> getPickMember() {
        List<UserInfo> pickBean = new ArrayList<>();
        for (PickGroupEntity entity : pickPos) {
            UserInfo userInfo = datas.get(entity.getParentPos()).getChildren().get(entity.getChildPos());
            pickBean.add(userInfo);
        }
        return pickBean;
    }

    @Override
    public void onBindChildViewHolder(BaseViewHolder holder, int groupPosition, int childPosition) {
        holder.setText(R.id.tv_name, datas.get(groupPosition).getChildren().get(childPosition).getNickname());
        ImageView ivAvatar = holder.itemView.findViewById(R.id.iv_avatar);
        File  file = datas.get(groupPosition).getChildren().get(childPosition).getAvatarFile();
        if(file != null)
//        PicUtils.loadAvatar(datas.get(groupPosition).getChildren().get(childPosition).getAvatarFile(),ivAvatar);
        datas.get(groupPosition).getChildren().get(childPosition).getAvatarBitmap(new GetAvatarBitmapCallback() {
            @Override
            public void gotResult(int i, String s, Bitmap bitmap) {
                if(i == 0){
                    ivAvatar.setImageBitmap(PicUtils.getCircleCropBitmap(bitmap));
                }else{
                    PicUtils.loadAvatar(datas.get(groupPosition).getChildren().get(childPosition).getAvatarFile(),ivAvatar);
                }
            }
        });
        if (pickModel == MODEL_PICK) {
            holder.setVisible(R.id.iv_pick, true);
            boolean isPick = false;
            for (PickGroupEntity entity : pickPos) {
                if (entity.getParentPos() == groupPosition && entity.getChildPos() == childPosition) {
                    isPick = true;
                }
            }
            holder.itemView.setSelected(isPick);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (isSinglePick) {
                        boolean hasSelected = false;
                        for (PickGroupEntity pickGroupEntity : pickPos) {
                            if (pickGroupEntity.getChildPos() == childPosition && pickGroupEntity.getParentPos() == groupPosition) {
                                hasSelected = true;
                                pickPos.remove(pickGroupEntity);
                            }
                        }
                        if (!hasSelected) {
                            pickPos.clear();
                            pickPos.add(new PickGroupEntity(groupPosition, childPosition));
                        }
                    } else {
                        boolean hasSelected = false;
                        for (PickGroupEntity pickGroupEntity : pickPos) {
                            if (pickGroupEntity.getChildPos() == childPosition && pickGroupEntity.getParentPos() == groupPosition) {
                                hasSelected = true;
                                pickPos.remove(pickGroupEntity);
                                break;
                            }
                        }
                        if (!hasSelected) {
                            pickPos.add(new PickGroupEntity(groupPosition, childPosition));
                        }
                    }
                    if (onItemSelectListener != null) {
                        List<UserInfo> pickBean = new ArrayList<>();
                        for (PickGroupEntity entity : pickPos) {
                            UserInfo userInfo = datas.get(entity.getParentPos()).getChildren().get(entity.getChildPos());
                            pickBean.add(userInfo);
                        }
                        onItemSelectListener.onSelect(pickBean);
                    }
                    notifyDataSetChanged();
                }
            });
        } else {
            holder.setVisible(R.id.iv_pick, false);
        }

    }

    public String getDraft(String convId) {
        return mDraftMap.get(convId);
    }

    public interface OnItemSelectListener {
        public void onSelect(List<UserInfo> userInfos);
    }
}
